package com.yuyou.fn.portal.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.github.wens.mybatisplus.toolkit.IdWorker;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.service.ITeacherService;
import com.yuyou.fn.portal.entity.OuterCollection;
import com.yuyou.fn.portal.mapper.OuterCollectionMapper;
import com.yuyou.fn.portal.service.IOuterCollectionService;
import com.yuyou.fn.portal.service.IOuterCourseClassService;
import com.yuyou.fn.portal.vo.CourseClassDetail;
import com.yuyou.fn.portal.vo.TeacherListResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 * OuterCollection 表数据服务层接口实现类
 *
 */
@Service
public class OuterCollectionServiceImpl extends SuperServiceImpl<OuterCollectionMapper, OuterCollection,Long> implements IOuterCollectionService {
    @Resource
    private IOuterCourseClassService outerCourseClassService;

    @Resource
    private ITeacherService teacherService;

    @Override
    public List<Object> findOuterCollection(int pageNo, int pageSize, Long userId) {
        Example<OuterCollection> outerCollectionExample=OuterCollection.createExample();
        Example.Criteria criteria=outerCollectionExample.createCriteria();
        criteria.andEqualTo("userId",userId);
        outerCollectionExample.orderBy("createTime").desc();
        Page<OuterCollection> outerCollectionPage=this.findPageByExample(outerCollectionExample,pageNo,pageSize);
        List<OuterCollection> outerCollectionList=outerCollectionPage.getRecords();
        List<Object> list=new ArrayList<Object>();
        for(OuterCollection outerCollection:outerCollectionList){
            if(outerCollection.getObjType()==0) {
                //OuterCourseClass outerCourseClass = outerCourseClassService.findByClassId(outerCollection.getObjId());
                CourseClassDetail courseClassDetail=outerCourseClassService.findClassDetailByClassId(outerCollection.getObjId());
                courseClassDetail.setOuterCollectionId(outerCollection.getOuterCollectionId());
               // BeanUtils.copyProperties(courseClassDetail,outerCourseClass);
                HashMap<String,Object> hashMap=new HashMap<String,Object>();
                hashMap.put("type",0);
                hashMap.put("collection",courseClassDetail);
                hashMap.put("createTime",outerCollection.getCreateTime());
                list.add(hashMap);
            }else if(outerCollection.getObjType()==1){
                Teacher teacher=teacherService.findById(outerCollection.getObjId());
                TeacherListResultVo teacherListResultVo=new TeacherListResultVo();
                BeanUtils.copyProperties(teacherListResultVo,teacher);
                teacherListResultVo.setOuterCollectionId(outerCollection.getOuterCollectionId());
                HashMap<String,Object> hashMap=new HashMap<String,Object>();
                hashMap.put("type",1);
                hashMap.put("collection",teacherListResultVo);
                hashMap.put("createTime",outerCollection.getCreateTime());
                list.add(hashMap);
              //  list.add(teacher);
            }
        }
        return  list;
    }

    @Transactional
    @Override
    public Long addOuterCollection(OuterCollection outerCollection, Long userId) {
        Example<OuterCollection> example=OuterCollection.createExample();

        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("objId",outerCollection.getObjId());
        criteria.andEqualTo("objType",outerCollection.getObjType());
        /*List<OuterCollection> outerCollections = this.findListByExample(example);*/
        if(this.countByExample(example)>0){
            throw  new BusException("已经收藏该内容");
        }
        outerCollection.setUserId(userId);
        outerCollection.setOuterCollectionId(IdWorker.getId());
        outerCollection.setUpdateTime(TimeUtils.now());
        outerCollection.setCreateTime(TimeUtils.now());
        if (outerCollection.getObjType() == 1){//记录教师被收藏数
            Teacher teacher1=teacherService.findById(outerCollection.getObjId());
            Teacher teacher=new Teacher();
            teacher.setTeacherId(teacher1.getTeacherId());
            teacher.setCollectionNum(teacher1.getCollectionNum()==null?1:teacher1.getCollectionNum()+1);
            teacherService.updateSelectiveById(teacher);
        }
        this.insertSelective(outerCollection);
        return  outerCollection.getOuterCollectionId();
    }

    @Override
    public void deleteOuterCollection(Long userId, List<Long> outerCollectionIds) {
        if(userId == null ){
            throw new NullPointerException();
        }
        Example<OuterCollection> example=OuterCollection.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andIn("outerCollectionId",outerCollectionIds);
        this.deleteByExample(example);
    }

    @Override
    public Long countNumByUserId(Long userId) {
        Example<OuterCollection> example=OuterCollection.createExample();
        example.createCriteria().andEqualTo("userId",userId);

        return this.countByExample(example);
    }

    @Override
    public OuterCollection findByUserIdAndObjType(Long userId,Long objId,Integer objType) {
        Example<OuterCollection> example=OuterCollection.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("objId",objId);
        criteria.andEqualTo("objType",objType);
        return this.findOneByExample(example);
    }
}